package org.dolphin.entity.security;

import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.ManyToOne;
import org.springframework.util.ObjectUtils;

@Embeddable
public class UserGroupPk implements Serializable {
  private static final long serialVersionUID = -3481296872756622213L;
  private User user;
  private Group group;

  @Override
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    } else if (obj instanceof UserGroupPk) {
      UserGroupPk oth = (UserGroupPk) obj;
      return ObjectUtils.nullSafeEquals(getUser(), oth.getUser())
          && ObjectUtils.nullSafeEquals(getGroup(), oth.getGroup());
    }
    return false;
  }

  @ManyToOne(cascade = {CascadeType.ALL})
  public Group getGroup() {
    return group;
  }

  @ManyToOne(cascade = {CascadeType.ALL})
  public User getUser() {
    return user;
  }

  @Override
  public int hashCode() {
    int hash = ObjectUtils.nullSafeHashCode(getUser());
    hash ^= ObjectUtils.nullSafeHashCode(getGroup());
    return hash;
  }

  public void setGroup(Group group) {
    this.group = group;
  }

  public void setUser(User user) {
    this.user = user;
  }
}
